package pers.sen.health.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import pers.sen.health.dto.CheckGroupDTO;
import pers.sen.health.entity.PageResult;
import pers.sen.health.entity.QueryPageBean;
import pers.sen.health.mapper.CheckGroupMapper;
import pers.sen.health.po.CheckGroup;
import pers.sen.health.po.CheckItem;
import pers.sen.health.service.CheckGroupService;
import pers.sen.health.service.CheckItemService;
import pers.sen.health.vo.CheckGroupVO;
import pers.sen.health.general.EmptyUtil;

import java.util.List;

/**
 * @author Jason
 * @version 1.0.0
 * @date 2020/11/6 20:18
 * @Description TODO
 */
@Service
@Transactional
public class CheckGroupServiceImpl extends ServiceImpl<CheckGroupMapper, CheckGroup> implements CheckGroupService {

    @Autowired
    private CheckItemService checkItemService;

    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        // 1. 构造条件
        QueryWrapper<CheckGroup> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_delete", 0);

        // 2. page方法返回查询结果
        Page<CheckGroup> page = null;
        if (StringUtils.isBlank(queryPageBean.getQueryString())) {
            page = page(new Page<CheckGroup>(queryPageBean.getCurrentPage(), queryPageBean.getPageSize()), queryWrapper);
        } else {
            queryWrapper.like("name", queryPageBean.getQueryString());
            page = page(new Page<CheckGroup>(queryPageBean.getCurrentPage(), queryPageBean.getPageSize()), queryWrapper);
        }

        // 3. 封装结果集
        PageResult pageResult = new PageResult(page.getTotal(), page.getRecords());
        return pageResult;
    }

    @Override
    public Boolean insertCheckGroup(CheckGroupDTO checkGroupDTO) {
        int checkGroupDTOId = checkGroupDTO.getId();
        // 主键不为空证明是修改，先删除中间表
        if (!EmptyUtil.isNullOrEmpty(checkGroupDTOId)) {
            baseMapper.deleteCheckItemInfoByGId(checkGroupDTOId);
        }

        // 添加操作
        boolean b = saveOrUpdate(checkGroupDTO);
        // 获取添加后的id
        Integer gid = checkGroupDTO.getId();
//        System.out.println("gid = " + gid);

        // 获取传入的检查项信息
        Integer[] checkitemIds = checkGroupDTO.getCheckitemIds();
        if (!EmptyUtil.isNullOrEmpty(checkitemIds)) {
            for (Integer checkitemId : checkitemIds) {
                // 遍历检查项，在中间表添加数据
                baseMapper.insertInfoByGid(gid, checkitemId);
            }
        }
        return b;
    }

    @Override
    public Boolean updateIsDeleteById(CheckGroup checkGroup) {
        //  逻辑删除
        checkGroup.setIs_delete(1);
        return updateById(checkGroup);
    }

    @Override
    public CheckGroupVO findCheckItemInfoByGroupId(int id) {
        // 查询所有检查项信息
        List<CheckItem> list = checkItemService.list();

        // 中间表查询检查组包含的检查项id
        Integer[] checkitemids = baseMapper.findCheckItemInfoByGroupId(id);
        //  将查询的结果集封装到vo 中
        CheckGroupVO checkGroupVO = new CheckGroupVO();
        checkGroupVO.setCheckItemList(list);
        checkGroupVO.setCheckitemIds(checkitemids);
        return checkGroupVO;
    }
}
